package org.cytoscape.myapp.cytoscape_app_v1.internal.grupos;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyTable;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewManager;

/**
 * Esta clase se agregaran metodos estaticos para acceder a informacion de la red
 * 
 */
 


public class ModelUtils {
	
	public static CyColumn getCyColumnNetwork(CyNetwork network, String nameColumn){
		
		CyTable table = network.getDefaultNodeTable();
		
		return table.getColumn(nameColumn);
		
	}
	
	public static HashMap<CyNode,List<Object>> getAllNodesWhitAttributes(CyNetwork network, String nameColumn){
		
		CyColumn columna = getCyColumnNetwork(network, nameColumn);
		
		List<CyNode> nodes = network.getNodeList();

		HashMap<CyNode,List<Object>> nodeAttributes = new HashMap<CyNode, List<Object>>(); 
		
		try{
				
				if (columna.getListElementType().equals(String.class)){
					for(CyNode node:nodes){				
						List<String> lista = network.getDefaultNodeTable().getRow(node.getSUID()).getList(nameColumn, String.class);
						List<Object> lisNodo = new Vector<Object>();
						for (String elem:lista)
							lisNodo.add(elem);
						nodeAttributes.put(node, lisNodo);					
					}
				}
				if (columna.getListElementType().equals(Integer.class)){
					for(CyNode node:nodes){				
						List<Integer> lista = network.getDefaultNodeTable().getRow(node.getSUID()).getList(nameColumn, Integer.class);
						List<Object> lisNodo = new Vector<Object>();
						for (Integer elem:lista)
							lisNodo.add(elem);
						nodeAttributes.put(node, lisNodo);					
					}
				}
			}
			catch (Exception e){
				System.out.println("No existe columna");
				e.printStackTrace();
			}
		return nodeAttributes;
	}
	
	public static Set<Object> getValuesCyColumnNetwork(CyNetwork network, String nameColumn){
		
		CyColumn columna = getCyColumnNetwork(network, nameColumn);
		
		Set<Object> values = new  HashSet<Object>();
		try {
			values.addAll(columna.getValues(columna.getType()));
		}
		catch (Exception e){
			e.printStackTrace();
			System.out.println("No Existe Columna");
		}
		return values;
	}

	public static HashMap<CyNode,List<Object>> getAllNodesWhitAttributesSimple(CyNetwork network, String nameColumn) {
		
		CyColumn columna = getCyColumnNetwork(network, nameColumn);
		
		List<CyNode> nodes = network.getNodeList();

		HashMap<CyNode,List<Object>> nodeAttributes = new HashMap<CyNode, List<Object>>(); 
		
		try{
			if (columna.getType().equals(String.class)){
				for(CyNode node:nodes){				
					Object lista = network.getDefaultNodeTable().getRow(node.getSUID()).get(nameColumn, String.class);
					List<Object> lisNodo = new Vector<Object>();
					lisNodo.add(lista);
					nodeAttributes.put(node, lisNodo);				
					}
				}
			}
			catch (Exception e){
				System.out.println("No existe columna");
				e.printStackTrace();
			}
		return nodeAttributes;
	}
	
}
